package nc.bs.util;

import java.util.ArrayList;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;

public class DaoOperateUtil {
	
	private static BaseDAO dao=null;
	public static BaseDAO getDao() {
		if(dao==null) {
			dao=new BaseDAO();
		}
		return dao;
	}
	
	public static String queryCloumn(String cloumn,String table,String field,String value) throws DAOException {
		String sql="select "+cloumn+" from "+table+" where "+field+"='"+value+"' and isnull(dr,0)=0";
		return (String) getDao().executeQuery(sql, new ColumnProcessor());
	}
	
	public static String queryDefdocCloumn(String cloumn,String listcode,String value) throws DAOException {
		String sql="select a."+cloumn+" from bd_defdoc a \r\n" + 
				"left join bd_defdoclist b on a.pk_defdoclist=b.pk_defdoclist \r\n" + 
				"where isnull(a.dr,0)=0 and b.code='"+listcode+"' and a.code='"+value+"'";
		return (String) getDao().executeQuery(sql, new ColumnProcessor());
	}
	
	public static String queryDefdocNameCloumn(String cloumn,String listcode,String value) throws DAOException {
		String sql="select a."+cloumn+" from bd_defdoc a \r\n" + 
				"left join bd_defdoclist b on a.pk_defdoclist=b.pk_defdoclist \r\n" + 
				"where isnull(a.dr,0)=0 and b.code='"+listcode+"' and a.name='"+value+"'";
		return (String) getDao().executeQuery(sql, new ColumnProcessor());
	}
	
	public static String queryMateClass(String pk,String notwhere) throws DAOException {
		String sql="select name from bd_marbasclass where pk_marbasclass='"+pk+"'";
		if(notwhere!=null&&!"".equals(notwhere)) {
			sql+=notwhere;
		}
		return (String) getDao().executeQuery(sql, new ColumnProcessor());
	} 
	
	public static String queryMateClassCode(String pk,String notwhere) throws DAOException {
		String sql="select code from bd_marbasclass where pk_marbasclass='"+pk+"'";
		if(notwhere!=null&&!"".equals(notwhere)) {
			sql+=notwhere;
		}
		return (String) getDao().executeQuery(sql, new ColumnProcessor());
	} 
	
	public static String queryCloumnSql(String sql) throws DAOException {
		
		Object object = getDao().executeQuery(sql, new ColumnProcessor());
		
		if (object==null) {
			return "";
		}else {
			return object.toString();
		}
		
		
	}

	@SuppressWarnings("unchecked")
	public static Object queryHeadVO(String table,String column,String pk,Class<?> obj) throws DAOException {
		String sql="select *from "+table+" where "+column+"='"+pk+"' and dr=0";
		ArrayList<Object> list = (ArrayList<Object>) getDao().executeQuery(sql, new BeanListProcessor(obj));
		return list==null||list.size()==0?obj:list.get(0);
	}	
}
